﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ASBCC.Database.Entity;
using ASBCC.Database.Service;
using System.Data;
using ASBCC.Workflow.K2Manager;
using ASBCC.Business;
using Telerik.Web.UI;
using BEGDC.CC.Web.Common;
using BEGDC.Utility.Extensions;


namespace BEGDC.CC.Web.Workflow.Admin
{
    public partial class FlowProcessStamp : System.Web.UI.Page
    {
        #region Define
        private ProcessBusiness processBusiness = new ProcessBusiness();
        #endregion

        #region PageLoad
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindProcNameDropdownlist();
                ucPager.Query();
            }
        }
        #endregion

        #region Event


        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            ucPager.Query();
        }

        protected void btnNew_Click(object sender, EventArgs e)
        {
            Response.Redirect("FlowProcessStampEdit.aspx");
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="source"></param>
        /// <param name="e"></param>
        protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)
        {
            Guid id = new Guid(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["ProcessStampID"].ToString());
            if (e.CommandName == "D")
            {
                Flow_ProcessStampManager.Delete(id);
                ucPager.Refresh();
            }
            else if (e.CommandName == "U")
            {
                //Response.Redirect("FlowProcessStampEdit.aspx?ID=" + id.ToString());
                //ucPager.Refresh();
                //Response.Write(string.Format("<script type=\"text/javascript\">window.showModalDialog('FlowProcessStampEdit.aspx?ID={0}',null ,'dialogWidth=800px;dialogHeight=600px;status=0;help=0');</script>", id));
                //return;
                this.ClientScript.RegisterClientScriptBlock(this.GetType(), "Alert", string.Format("<script type=\"text/javascript\">window.showModalDialog('FlowProcessStampEdit.aspx?ID={0}',null ,'dialogWidth=800px;dialogHeight=600px;status=0;help=0');</script>", id), true);
            }
        }

        #endregion

        #region Function

        protected void ucPager_OnNeedDataSource(object sender, PagerEventArgs e)
        {
            string procName = ddlProcName.SelectedValue;
            string applicationNo = txtApplicationNo.Text.Trim().SanitizeSqlValue();

            string condition = string.Empty;
            object count = 0;

            //if (ddlProcName.SelectedIndex > 0)
            //{
                condition += string.Format(" and ProcName='{0}' ", ddlProcName.SelectedValue);
            //}

            if (txtApplicationNo.Text.Trim() != string.Empty)
            {
                condition += string.Format(" and ApplicationNo like '%{0}%' ", txtApplicationNo.Text.Trim().SanitizeSqlValue());
            }

            ucPager.DataSource = DatabaseManager.usp3_SelectDataForSearchPage("Flow_ProcessStamp", e.IsLast, "CreatedDate", ucPager.PageIndex, ucPager.PageSize, ref count, "*", condition);

            ucPager.RowsCount = (int)count;
        }

        private void BindProcNameDropdownlist()
        {
            Flow_ProcessTableEntitySet set = Flow_ProcessManager.SelectAll();
            DataTable dt = null;
            if (set != null && set.Tables.Count > 0)
            {
                dt = set.Tables[0];
            }
            //if (dt != null)
            //{
            //    DataRow dr = dt.NewRow();
            //    dr["ProcName"] = "";
            //    dr["ProcDisplayEn"] = "";
            //    dt.Rows.InsertAt(dr, 0);
            //}

            DataView dv = new DataView(dt);
            dv.Sort = "ProcName";

            ddlProcName.DataTextField = "ProcDisplayEn";
            ddlProcName.DataValueField = "ProcName";
            ddlProcName.DataSource = dv;
            ddlProcName.DataBind();
        }
        #endregion

       
    }
}
